package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import com.qk.dto.CourseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> list(CourseDto courseDto) {
        PageHelper.startPage(courseDto.getPage(),courseDto.getPageSize());
        List<Course> courseList = courseMapper.selectList(courseDto);
        Page<Course> page =  (Page<Course>) courseList;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void add(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        course.setCreateTime(LocalDateTime.now());
        courseMapper.insert(course);

    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);
    }

    @Override
    public List<Course> allList(Course course) {
        return courseMapper.alldeptList();
    }

    @Override
    public List<Course> ListBySubject(Integer subject) {
        return courseMapper.selectlist(subject);
    }

}
